package com.example.shop_management.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.shop_management.entity.Admin;
import com.example.shop_management.entity.User;
import com.example.shop_management.service.impl.AdminServiceImpl;
import com.example.shop_management.service.impl.UserServiceImpl;
import com.example.shop_management.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @GetMapping("findall")
    public List<User> findall() {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.orderByDesc("create_time");
        return userService.list(userQueryWrapper);
    }


    @PostMapping("save")
    public boolean insert(@RequestBody User user){
        user.setPassword(MD5Util.getMD5(user.getPassword()));
         return userService.saveOrUpdate(user);
    }

    @PostMapping("delete")
    public boolean delete(@RequestBody int id){
        return userService.removeById(id);
    }

    @PostMapping("deleltebatch")
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return userService.removeByIds(ids);
    }

    //条件查询
    @GetMapping("selectbycondition")
    public List<User> selectByCondition(@RequestParam(defaultValue = "",required = false) String username,
                                        @RequestParam(defaultValue = "",required = false) String uname,
                                        @RequestParam(defaultValue = "",required = false) String gender){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if(!"".equals(username)){
            userQueryWrapper.like("username",username);
        }
        if(!"".equals(uname)){
            userQueryWrapper.like("uname",uname);
        }
        if(!"".equals(gender)){
            userQueryWrapper.like("gender",gender);
        }
        return userService.list(userQueryWrapper);
    }

    @GetMapping("selectbyid")
    public User selectbyid(@RequestParam String id){
        return userService.getById(id);
    }


}

